// wether.c
//
// Copyright (C) 2013 - jyh
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//           wether.c
//  Sun April 07 08:53:26 2013
//  Copyright  2013  jyh
//  <user@host>

#include "weather.h"

#define MAX_LEN 1024;

bool getWebtoTmpFile(char *url){
	CURL *curl;
	CURLcode res;
	FILE *tmpfile;
	tmpfile = fopen("tmpfile","w");

	curl = curl_easy_init();
	if (curl){
		curl_easy_setopt(curl,CURLOPT_URL,url);
		curl_easy_setopt(curl,CURLOPT_WRITEDATA,tmpfile);
		
		res = curl_easy_perform(curl);
		if (res != CURLE_OK){
			fprintf(stderr,"curl_eary_perform() failed:%s\n",
						curl_easy_strerror(res));
			return false;
		}
		curl_easy_cleanup(curl);
	}
	else
	  return false;

	fflush(tmpfile);
	fclose(tmpfile);
	return true;
}

void readfromTmpfile(char *path,char *s){
	/*read from a xml file*/
	FILE *ptmpfile;
	long len;
	size_t result;
	
	ptmpfile = fopen(path,"r");	
	if(NULL != ptmpfile){
		fseek(ptmpfile,0,SEEK_END);
		len = ftell(ptmpfile);
		rewind(ptmpfile);
		result = fread(s,sizeof(char),len,ptmpfile);
		s[len] = '\0';
		if(result != len){
			perror("error to read tmp file!");
			return;
		}
	}
	else{
		perror("error to write tmp file!");
		fclose(ptmpfile);
		return ;
	}
	fclose(ptmpfile);//why undefined reference to 'fclose'?!
	return;
}

void paraseWeatherInfo(char info[21][255]){
	char tmpfilestr[1024];
	char *pch_start,*pch_end;
	int str_len;
	char infotmp[255];

	readfromTmpfile("tmpfile",tmpfilestr);
	printf("%s",tmpfilestr);

	//问题在这里2013.05.26晚上11：00^_^
/*	int pch = 0;
	int num = 0;
	while(tmpfilestr[pch] != '\0'){
		if (tmpfilestr[pch] != '\“'){
			pch++;
		}
		else{
			num++;
			if (num%2 == 1)
			  pch_start = pch + 1;
			else{
			  pch_end = pch - 1;
			  strncpy(info[num/2-1][255],pch_start,pch_end-pch_start);
			  char tmpaaa[255];
			  strncpy(tmpaaa,pch_start,pch_end-pch_start);
			  printf("%s",tmpaaa);
			}
			pch++;
		}
 	}*/
	return;
}
